package com.android.support.lib.universal.view.general;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.android.support.lib.universal.util.UtilAssets;
import com.android.support.lib.universal.util.UtilProportion;

/**
 * Created by Administrator on 2017/5/11.
 */

public class RatingStart extends View {

    public RatingStart(Context context) {
        super(context);
        create(context);
    }

    public RatingStart(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        create(context);
    }

    private Bitmap bitmapBG;
    private Bitmap bitmap;
    private float rating = 2.5f;
    private float minRating = 0.5f;//最少星星个数。即最小单位
    private float maxRating = 5;//最大星星个数
    private float offset = 3;//星星之间的间隙。

    //设置评分等级，0~100
    public void setRating(float rating, boolean isShowBg) {
        if (rating < minRating) {
            rating = minRating;
        }
        if (rating > maxRating) {
            rating = maxRating;
        }
        this.rating = rating;
        this.isShowBg = isShowBg;
        invalidate();
    }

    private boolean isShowBg = false;//是否显示背景暗色星星。

    private void create(Context context) {
        bitmapBG = UtilAssets.getInstance(context).getBitmapFromAssets(getClass().getName(), "进度条/ratingbar_bg.png", 0);
        bitmapBG = UtilProportion.getInstance((Activity) context).adapterBitem(bitmapBG);
        bitmap = UtilAssets.getInstance(context).getBitmapFromAssets(getClass().getName(), "进度条/ratingbar.png", 0);
        bitmap = UtilProportion.getInstance((Activity) context).adapterBitem(bitmap);
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension((int) (bitmap.getWidth() * maxRating + maxRating * offset), bitmap.getHeight());
    }

    Paint paint;

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        if (paint == null) {
            paint = new Paint();
            paint.setAntiAlias(true);
            paint.setDither(true);
        }
        paint.setXfermode(null);
        int x = 0;
        if(isShowBg){
            for (int i = 0; i < maxRating; i++) {
                canvas.drawBitmap(bitmapBG, x, 0, paint);
                x = (int) (x + bitmapBG.getWidth() + offset);
            }
            canvas.saveLayer(0, 0, x, bitmapBG.getHeight(), paint, Canvas.ALL_SAVE_FLAG);
        }
        x = 0;
        for (int i = 0; i < maxRating; i++) {
            canvas.drawBitmap(bitmap, x, 0, paint);
            x = (int) (x + bitmap.getWidth() + offset);
        }
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        float add = offset * (int) rating;
        int pro = (int) ((bitmap.getWidth() * maxRating * rating / maxRating) + add);
        Rect rect = new Rect(0, 0, pro, bitmap.getHeight());
        canvas.drawRect(rect, paint);
        rect = new Rect(pro, 0, x, bitmap.getHeight());
        paint.setXfermode(new PorterDuffXfermode((PorterDuff.Mode.CLEAR)));
        canvas.drawRect(rect, paint);
    }
}
